package com.x.erp.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.x.erp.model.InsuranceClue;
import com.x.framework.dao.BaseDao;
/**
 * 保险线索dao
 * @author pansong
 *
 */
@Repository
public class InsuranceClueDao extends BaseDao {
	/**
	 * 保险线索列表
	 * @param insuranceClue
	 * @return
	 */
	private final static String selectInsuranceClueListSql = "SELECT T1.*, T2.MODEL_NAME, T3.CITY_NAME "
			+ "FROM ERP_INSURANCE_CLUE T1 "
			+ "LEFT JOIN x_base.BASE_MODEL T2 ON T1.MODEL_ID = T2.MODEL_ID "
			+ "LEFT JOIN BASE_CITY T3 ON T1.CITY_ID = T3.CITY_ID " + "WHERE 1 = 1 ";

	public List<InsuranceClue> selectInsuranceClueList(InsuranceClue insuranceClue) throws Exception {
		StringBuilder sql = new StringBuilder(selectInsuranceClueListSql);
		List<Object> params = new ArrayList<>();
		selectSearch(sql, params, insuranceClue);
		sql.append("ORDER BY CLUE_CREATE_TIME DESC ");
		return this.queryForList(sql.toString(), InsuranceClue.class, params, insuranceClue);
	}

	/**
	 * 保险线索总数
	 * 
	 * @param insuranceClue
	 * @return
	 */
	private final static String selectInsuranceClueNumSql = "SELECT COUNT(T1.CLUE_ID) FROM ERP_INSURANCE_CLUE T1 WHERE 1 = 1 ";

	public int selectInsuranceClueNum(InsuranceClue insuranceClue) {
		StringBuilder sql = new StringBuilder(selectInsuranceClueNumSql);
		List<Object> params = new ArrayList<>();
		selectSearch(sql, params, insuranceClue);
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 拼接查询条件
	 */
	private void selectSearch(StringBuilder sql, List<Object> params, InsuranceClue insuranceClue) {
		if (this.isNotNull(insuranceClue.getClueId())) {
			sql.append("AND T1.CLUE_ID = ? ");
			params.add(insuranceClue.getClueId());
		}
		if (this.isNotNull(insuranceClue.getModelId())) {
			sql.append("AND T1.MODEL_ID = ? ");
			params.add(insuranceClue.getModelId());
		}
		if (this.isNotNull(insuranceClue.getCityId())) {
			sql.append("AND T1.CITY_ID = ? ");
			params.add(insuranceClue.getCityId());
		}
		if (this.isNotNull(insuranceClue.getClueLicenceStatus())) {
			sql.append("AND T1.CLUE_LICENCE_STATE = ? ");
			params.add(insuranceClue.getClueLicenceStatus());
		}
		if (this.isNotNull(insuranceClue.getClueCarPlant())) {
			sql.append("AND T1.CLUE_CAR_PLANT = ? ");
			params.add(insuranceClue.getClueCarPlant());
		}
		if (this.isNotNull(insuranceClue.getClueMobile())) {
			sql.append("AND T1.CLUE_MOBILE = ? ");
			params.add(insuranceClue.getClueMobile());
		}
		if (this.isNotNull(insuranceClue.getClueIdCard())) {
			sql.append("AND T1.CLUE_ID_CARD = ? ");
			params.add(insuranceClue.getClueIdCard());
		}
	}
}
